import requests
import re

class Weather(object):
    def __init__(self):
        self.url = 'http://www.weather.com.cn/weather1d/101110101.shtml'
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'
        }

    def get_data(self):
        response = requests.get(self.url, self.headers)
        response.encoding = 'utf-8'
        html = response.text
        ADDRESS = re.findall('<title>(.*?)</title>', html)
        AIM = re.findall('<input type="hidden" id="hidden_title" value="(.*?)月(.*?)日(.*?)时(.*?) (.*?)  (.*?)  (.*?)"',
                         html, re.S)
        AIR_DATA = re.findall('<li class="li6 hot">\n<i></i>\n<span>(.*?)</span>\n<em>(.*?)</em>\n<p>(.*?)</p>\n</li>',
                              html, re.S)
        RADITION = re.findall('<li class="li1 hot">\n<i></i>\n<span>(.*?)</span>\n<em>(.*?)</em>\n<p>(.*?)</p>\n</li>',
                              html, re.S)
        SPORTS = re.findall('<li class="li2 hot">\n(.*?)\n<em>(.*?)</em>\n<p>(.*?)</p>\n</a>\n</li>', html, re.S)
        CLOSE = re.findall('<li class="li3 hot" id="chuanyi">\n(.*?)\n<span>(.*?)</span>\n<em>(.*?)</em>\n<p>(.*?)</p>\n</a>\n</li>',
                           html,re.S)
        #print(ADDRESS[0][0:4])
        print("{} {}月{}日 {}".format(ADDRESS[0][:4], AIM[0][0], AIM[0][1], AIM[0][4]))
        #print("更新时间：{}:00".format(AIM[0][2]))
        print("今日天气：{}".format(AIM[0][5]))
        print("今日温度：{}".format(AIM[0][6]))
        #print("空气质量：{}".format(AIR_DATA[0][0]))
        print("防晒指数：{}".format(RADITION[0][-1]))
        print("运动指数：{}".format(SPORTS[0][-1]))
        print("穿衣指数：{}".format(CLOSE[0][1] + '，' +CLOSE[0][-1]))

if __name__ == '__main__':
    Weather = Weather()
    Weather.get_data()
